<?php

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {

    public function _initRequest() {
        $frontController = Zend_Controller_Front::getInstance();
        //$frontController->throwExceptions(false);
        $frontController->setControllerDirectory(array(
            'default' 		=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'controllers',
            'admin' 		=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'common' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'controllers',
            'user' 			=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'user' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'controllers',
            'admin_user' 	=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'user' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'controllers',
            'cms' 			=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'cms' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'controllers',
            'admin_cms' 	=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'cms' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'controllers',
            'checkout' 		=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'checkout' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'controllers',
            'report' 		=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'report' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'controllers',
			'bid' 			=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'bid' . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR . 'controllers',
			'admin_bid' 	=> APPLICATION_PATH . DIRECTORY_SEPARATOR . 'modules' . DIRECTORY_SEPARATOR . 'bid' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR . 'controllers'
        ));

        //Initialize and/or retrieve a ViewRenderer object on demand via the helper broker
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->initView();

        //add the global helper directory path
        $viewRenderer->view->addHelperPath(APPLICATION_PATH.DIRECTORY_SEPARATOR. 'modules' . DIRECTORY_SEPARATOR .'common/public/views/helpers');
        $viewRenderer->view->addHelperPath(APPLICATION_PATH.DIRECTORY_SEPARATOR. 'modules' . DIRECTORY_SEPARATOR .'common/admin/views/helpers');
		$viewRenderer->view->addHelperPath(APPLICATION_PATH.DIRECTORY_SEPARATOR. 'modules' . DIRECTORY_SEPARATOR .'cms/public/views/helpers');
		$viewRenderer->view->addHelperPath(APPLICATION_PATH.DIRECTORY_SEPARATOR. 'modules' . DIRECTORY_SEPARATOR .'cms/admin/views/helpers');

        Zend_Registry::set('default_language', "en");
        Zend_Registry::set('page_size', 10);
        Zend_Registry::set('page_deal_size', 5);
        Zend_Registry::set('deal_status', array(0=>'Open', 1=>'Status', 2=>'Status', 3=>'Close'));
        
        Zend_Registry::set('page_sold_deal_size', 10);
		Zend_Registry::set('fb_appId', '211916795501141');
		Zend_Registry::set('fb_secret', '3f810354b43f3be22f3e483798a17508');
        
        Zend_Registry::set('order_status', array(1 => "Chờ thanh toán", 2 => "Đang xử lý", 3 => "Hoàn tất", 4 => "Hủy"));
        Zend_Registry::set('barcode_status', array(0=>'No', 1=>'Yes'));
        Zend_Registry::set('comment_status', array(0 => "Chưa duyệt", 1 => "Đã duyệt"));
    }

    protected function _initDb() {

        $dbOption = $this->getOption('resources');
        $dbOption = $dbOption['db'];

        // Setup database
        $db = Zend_Db::factory($dbOption['adapter'], $dbOption['params']);

        $db->setFetchMode(Zend_Db::FETCH_ASSOC);
        $db->query("SET NAMES 'utf8'");
        $db->query("SET CHARACTER SET 'utf8'");

        Zend_Registry::set('connectDB', $db);

        //Khi thiet lap che do nay model moi co the su dung duoc
        Zend_Db_Table::setDefaultAdapter($db);

        //
        Zend_Db_Table_Abstract::setDefaultAdapter($db);

        // Return it, so that it can be stored by the bootstrap
        return $db;
    }

    protected function _initRouter() {
        $front = Zend_Controller_Front::getInstance();
        $router = $front->getRouter();

        $path = APPLICATION_PATH . '/configs/routes.ini';
        $config = new Zend_Config_Ini($path, 'production');
        $router->addConfig($config, 'routes');
    }

    protected function _initTranslate() {
        // We use the Swedish locale as an example
        $locale = new Zend_Locale('en');
        Zend_Registry::set('Zend_Locale', $locale);

        // Set up and load the translations (all of them!)
        $translate = new Zend_Translate('csv', APPLICATION_PATH . DIRECTORY_SEPARATOR . 'languages/en.csv', 'en');
        $translate->addTranslation(APPLICATION_PATH . DIRECTORY_SEPARATOR . 'languages/vi.csv', 'vi');

        //$translate->setLocale($langLocale); // Use this if you only want to load the translation matching current locale, experiment.
        // Save it for later
        $registry = Zend_Registry::getInstance();
        $registry->set('Zend_Translate', $translate);
    }

    public function _initAutoloader() {
        $autoloader = Zend_Loader_Autoloader::getInstance();
        $autoloader->registerNamespace('My_');
        $autoloader->suppressNotFoundWarnings(true);
        
        $front = Zend_Controller_Front::getInstance();
        $front->registerPlugin(new My_Controller_Plugin_Multilanguage());
        $front->registerPlugin(new My_Controller_Plugin_Response(),150);

        return $autoloader;
    }
    
	protected function _initViewHelpers()
	{
		$this->bootstrap("frontController");
        $front=$this->getResource("frontController");
        $request=new Zend_Controller_Request_Http();
        $front->setRequest($request);
        $baseUrl = Zend_Controller_Front::getInstance()->getBaseUrl();
        $baseUrl = str_replace("/index.php", "", $baseUrl);

		
		$viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->initView();        
	   
	    $viewRenderer->view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper");
	    $viewRenderer->view->jQuery()->addStylesheet($baseUrl.'/public/css/ui-lightness/jquery-ui-1.8.9.custom.css')
	        ->setLocalPath($baseUrl.'/public/js/jquery-1.5.min.js')
	        ->setUiLocalPath($baseUrl.'/public/js/jquery-ui-1.8.9.custom.min.js')
	        ->enable()->uiEnable();
	}
}
